<%
layout("/layouts/platform.html"){
%>
<style>  .modal-backdrop {
    filter: alpha(opacity=0)!important;
    opacity: 0!important;
}
</style>
<section class="content-wrap bg-white">
    <header class="header navbar bg-white shadow">
        <div class="btn-group tool-button">
            <a id="add" class="btn btn-primary navbar-btn" href="${base}/platform/customer/customer/add" data-pjax><i class="ti-plus"></i> 新建客户</a>
            <button class="btn btn-danger navbar-btn" onclick="delCheck()"><i class="ti-close"></i> 删除选中</button>
            <button class="btn btn-danger navbar-btn" onclick="knotOrder()"><i class="ti-close"></i> 结单</button>
        </div>
        <div class="pull-right offscreen-right">
            <button class="btn btn-primary navbar-btn" onclick="sublime.toggleFilter('.cd-panel')"><i
                    class="fa fa-sliders"></i> 筛选
            </button>
        </div>
    </header>
    <div class=panel-body style="padding-top: 50px;">
        <div class="table-responsive no-border">
            <table class="table table-bordered table-striped mg-t datatable">
                <thead>
                    <tr>
                            <th>公司名称</th>
                            <th>等级</th>
                            <th>联系电话</th>
                            <th>地址</th>
                            <th>关键词</th>
                            <th>联系人</th>
                            <th>主营业务</th>
                            <th>操作</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</section>
<div class="cd-panel from-right">
    <header class="cd-panel-header">
        <h4>高级筛选</h4>
    </header>
    <div class="cd-panel-container">
        <div class="cd-panel-content shadow">
            <div class="form-group">
                <label for="companyName">公司名称</label>
                <input type="text" id="companyName" name="companyName" onclick="this.value=''" class="form-control" placeholder="公司名称" autofocus>
            </div>
            <div class="form-group">
                <label for="contactsName">联系人</label>
                <input type="text" id="contactsName" name="contactsName" onclick="this.value=''" class="form-control" placeholder="联系人/负责人">
            </div>
            <button id="searchBtn" type="button" class="btn btn-default">查询</button>
        </div>
    </div>
</div>
<div id="dialogDelete" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">删除</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        删除后无法恢复。<br/>
                        <br/>确定删除吗？
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取  消</button>
                <button id="okDel" type="button" class="btn btn-primary" data-loading-text="正在删除...">确  定</button>
            </div>
        </div>
    </div>
</div>

<div id="dialogKnotOrder" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">结单</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                         <ul id="liName"></ul>
                        <br/>确定结单以上客户吗？
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取  消</button>
                <button id="okKnotOrder" type="button" class="btn btn-primary" data-loading-text="正在结单...">确  定</button>
            </div>
        </div>
    </div>
</div>

<div id="dialogToPublic" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">添加公共客户库</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-xs-12">
                        <br/>确定转入公共客户库？
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取  消</button>
                <button id="okToPublic" type="button" class="btn btn-primary" data-loading-text="正在转入...">确  定</button>
            </div>
        </div>
    </div>
</div>
<!--data-backdrop="static" -->
<div id="dialogDetail" class="modal fade bs-modal-sm" tabindex="-3" role="dialog" aria-hidden="true" >
    <div id="myModal" class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>
<div id="dialogAddDocumentary" class="modal fade bs-modal-sm" tabindex="-4" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        </div>
    </div>
</div>
<script language="JavaScript">
    var datatable;
    function initDatatable() {
        datatable = $('.datatable').DataTable({
            "dom": '<"toolbar">frtip',
            "searching":false,
            "processing": false,
            "serverSide": true,
            "select": true,
            "ordering": true,
            "language": {
                "url": "${base}/assets/plugins/datatables/cn.json"
            },
            "preDrawCallback": function () {
                sublime.showLoadingbar($(".main-content"));
            },
            "drawCallback": function () {
                sublime.closeLoadingbar($(".main-content"));
            },
            "createdRow": function( row, data, dataIndex ) {
                if(data.mainBusiness.length > 50){//只有超长，才有td点击事件
                    $(row).children('td').eq(6).attr('onclick','javascript:changeShowRemarks(this);');
                }
                $(row).children('td').eq(6).attr('content',data.mainBusiness);
            },
            "ajax": {
                "url": "${base}/platform/customer/customer/data",
                "type": "post",
                "data": function (d) {
                    //d.nutz = $('#nutz').val();
                    d.companyName=$("#companyName").val();
                    d.contactsName=$("#contactsName").val();
                    d.state=1;
                }
            },
            "order": [[0, "desc"]],
            "columns": [
                    {"data": "companyName", "bSortable": true,"sWidth":"15%"},
                    {"data": "level", "bSortable": true,"sWidth":"5%"},
                    {"data": "telphone", "bSortable": true,"sWidth":"8%"},
                    {"data": "address", "bSortable": true,"sWidth":"15%"},
                    {"data": "keyword", "bSortable": true,"sWidth":"10%"},
                    {"data": "contactsName", "bSortable": true,"sWidth":"15%"},
                    {"data": "mainBusiness", "bSortable": true,"sWidth":"28%"},
            ],
            "columnDefs": [
                {
                    "render": function (data, type, row) {
                        return '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">' +
                                ' <i class="ti-settings"></i> <span class="ti-angle-down"></span></button><ul class="dropdown-menu" role="menu">' +
                                '<li><a href="${base}/platform/customer/customer/detail/' + row.id + '" data-toggle="modal" data-target="#dialogDetail">查看</a></li>' +
                                '<li><a href="${base}/platform/customer/customer/edit/' + row.id + '" data-pjax>修改</a></li>' +
                                '<li><a  href="${base}/platform/customer/documentary/add/' + row.id + '" data-toggle="modal" data-target="#dialogAddDocumentary">添加跟单</a></li>' +
                                '<li class="divider"></li>' +
                                '<li><a href="javascript:;" onclick="del(\'' + row.id + '\')">删除</a></li>' +
                                '<li><a href="javascript:;" onclick="toPublic(\'' + row.id + '\')">添加到公共客户库</a></li>' +
                                '</ul></div>';
                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row) {
                        if (data) {
                            return '<a href="${base}/platform/customer/customer/detail/' + row.id + '" data-toggle="modal" data-target="#dialogDetail"  style="color: blue">'+row.companyName+'</a>';
                        }
                        return '';
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row) {
                        if (row.mainBusiness.length>50) {
                            return getPartialRemarksHtml(row.mainBusiness);
                        }else{
                            return row.mainBusiness;
                        }
                        return '';
                    },
                    "targets": 6
                }
            ]
        });
        datatable.on('click', 'tr', function () {
            $(this).toggleClass('selected');
        });
        $("#searchBtn").on('click', function () {
            datatable.ajax.reload();
        });
    }
    function del(id) {
        var dialog = $("#dialogDelete");
        dialog.modal("show");
        dialog.find("#okDel").unbind("click");
        dialog.find("#okDel").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/customer/customer/delete/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }

    function toPublic(id) {
        var dialog = $("#dialogToPublic");
        dialog.modal("show");
        dialog.find("#okToPublic").unbind("click");
        dialog.find("#okToPublic").bind("click", function (event) {
            var btn = $(this);
            btn.button("loading");
            $.post("${base}/platform/customer/customer/toPublic/" + id, {}, function (data) {
                if (data.code == 0) {
                    datatable.ajax.reload(null,false);
                } else {
                    Toast.error(data.msg);
                }
                //重置按钮状态，关闭提示框
                btn.button("reset");
                dialog.modal("hide");
            }, "json");
        });
    }
    function delCheck() {
        var chks = datatable.rows('.selected').data();
        if (chks.length > 0) {
            var ids = [];
            $.each(datatable.rows('.selected').data(), function (i, n) {
                ids.push(n.id);
            });
            var dialog = $("#dialogDelete");
            dialog.modal("show");
            dialog.find("#okDel").unbind("click");
            dialog.find("#okDel").bind("click", function (event) {
                var btn = $(this);
                btn.button("loading");
                $.post("${base}/platform/customer/customer/delete", {ids: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        datatable.ajax.reload(null,false);
                    } else {
                        Toast.error(data.msg);
                    }
                    btn.button("reset");
                    dialog.modal("hide");
                }, "json");
            });
        } else {
            Toast.warning("请先选择要删除的项！");
        }
    }

    function knotOrder() {
        var chks = datatable.rows('.selected').data();
        if (chks.length > 0) {
            var ids = [];
            $("#liName").html("");
            $.each(datatable.rows('.selected').data(), function (i, n) {
                ids.push(n.id);
                $("#liName").append("<li>"+n.companyName+"</li>");
            });

            var dialog = $("#dialogKnotOrder");
            dialog.modal("show");
            dialog.find("#okKnotOrder").unbind("click");
            dialog.find("#okKnotOrder").bind("click", function (event) {
                var btn = $(this);
                btn.button("loading");
                $.post("${base}/platform/customer/customer/knotOrder", {ids: ids.toString()}, function (data) {
                    if (data.code == 0) {
                        datatable.ajax.reload(null,false);
                    } else {
                        Toast.error(data.msg);
                    }
                    btn.button("reset");
                    dialog.modal("hide");
                }, "json");
            });
        } else {
            Toast.warning("请先选择要结单的项！");
        }
    }
    $(function () {
        initDatatable();
        $("#dialogDetail").on("hidden.bs.modal", function () {
            $(this).removeData("bs.modal");
        });
       // $("#myModal").draggable();//为模态对话框添加拖拽
        //$("#dialogDetail").css("overflow", "hidden");//禁止模态对话框的半透明背景滚动
    });
    //部分备注信息
    function getPartialRemarksHtml(remarks){
        return remarks.substr(0,50) + '<a href="javascript:void(0);" style="color: blue"><b>...</b></a>';
    }

    //全部备注信息
    function getTotalRemarksHtml(remarks){
        return remarks + '&nbsp;&nbsp;<a href="javascript:void(0);" style="color: blue" >收起</a>';
    }

    //切换显示备注信息，显示部分或者全部
    function changeShowRemarks(obj){//obj是td
        var content = $(obj).attr("content");
        if(content != null && content != ''){
            if($(obj).attr("isDetail") == 'true'){//当前显示的是详细备注，切换到显示部分
                //$(obj).removeAttr('isDetail');//remove也可以
                $(obj).attr('isDetail',false);
                $(obj).html(getPartialRemarksHtml(content));
            }else{//当前显示的是部分备注信息，切换到显示全部
                $(obj).attr('isDetail',true);
                $(obj).html(getTotalRemarksHtml(content));
            }
        }
    }
</script>

<%}%>